﻿using SupportLayer.Enums;
using SupportLayer.Objetos.Placa;
using SupportLayer.Util.ExtensionMethods;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace TranSys_Web.UCs
{
  public class LoadPictograma
  {
    private int GapHeight;
    private PlacaClass Placa;
    private double Divideby;

    public LoadPictograma(int _gapHeight, PlacaClass _placa, double divideby)
    {
      GapHeight = _gapHeight;
      Placa = _placa;
      Divideby = divideby;
    }

    public Panel PictogramaSimples(LinhaClass linhaClass, int y, TipoSeta _tipoSeta, bool setaAnteriorEsquerda)
    {
      Panel pnlImagem = new Panel();

      switch (_tipoSeta)
      {
        case TipoSeta.S1O:
        case TipoSeta.S1NO:
        case TipoSeta.S1SO:
        case TipoSeta.S2NO:
        case TipoSeta.S2SO:
          pnlImagem.Style.Add("float", "left");
          pnlImagem.Style.Add("margin-left", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          break;

        case TipoSeta.S1N:
        case TipoSeta.S2N:
          if (setaAnteriorEsquerda)
          {
            pnlImagem.Style.Add("float", "right");
            pnlImagem.Style.Add("margin-right", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          }
          else
          {
            pnlImagem.Style.Add("float", "left");
            pnlImagem.Style.Add("margin-left", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          }
          break;

        case TipoSeta.S1E:
        case TipoSeta.S1NE:
        case TipoSeta.S1SE:
        case TipoSeta.S2NE:
        case TipoSeta.S2SE:
          pnlImagem.Style.Add("float", "right");
          pnlImagem.Style.Add("margin-right", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          break;

        default:
          break;
      }

      pnlImagem.Style.Add("height", ((linhaClass.Height + GapHeight) / Divideby).toStringNumber() + "px");
      pnlImagem.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");

      Panel pnlPictograma = new Panel();
      pnlPictograma.Style.Add("height", "100%");
      pnlPictograma.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
      pnlPictograma.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
      pnlPictograma.Style.Add("background-size", "100%");
      pnlImagem.Controls.Add(pnlPictograma);
      return pnlImagem;
    }

    public Panel PictogramaDuplo(int posLinha, int y, TipoSeta _tipoSeta, bool setaAnteriorEsquerda, int quantidadeAgrupamentos)
    {
      Panel pnlImagem = new Panel();
      Panel pnlPictograma = new Panel();
      List<Panel> lstPictogramas = new List<Panel>();
      LinhaClass linhaClass = Placa.Linhas[posLinha];

      switch (_tipoSeta)
      {
        case TipoSeta.S2NO:
        case TipoSeta.S2SO:
          pnlImagem.Style.Add("float", "left");
          pnlImagem.Style.Add("margin-left", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          break;

        case TipoSeta.S2N:
          if (setaAnteriorEsquerda)
          {
            pnlImagem.Style.Add("float", "right");
            pnlImagem.Style.Add("margin-right", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          }
          else
          {
            pnlImagem.Style.Add("float", "left");
            pnlImagem.Style.Add("margin-left", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          }
          break;

        case TipoSeta.S2NE:
        case TipoSeta.S2SE:
          pnlImagem.Style.Add("float", "right");
          pnlImagem.Style.Add("margin-right", (linhaClass.Gap / Divideby).toStringNumber() + "px");
          break;

        default:
          break;
      }

      if (quantidadeAgrupamentos == 2)
      {
        pnlImagem.Style.Add("height",
          ((Placa.Linhas[posLinha].Height +
          Placa.Linhas[posLinha + 1].Height +
          Placa.Linhas[posLinha + 2].Height -
          linhaClass.Gap * 2) / Divideby).toStringNumber() + "px");

        Panel pnl1 = new Panel();
        Panel pnl2 = new Panel();
        Panel pnl3 = new Panel();

        if (linhaClass.AgruparPictograma)
        {
          if (Placa.Linhas[posLinha + 1].AgruparPictograma)
          {
            pnl1.Style.Add("height", "100%");
            pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl1.Style.Add("background-size", "100%");
            lstPictogramas.AddRange(new List<Panel>() { pnl1 });
          }
          else
          {
            pnl1.Style.Add("height", "66.6%");
            pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl1.Style.Add("background-size", "100%");

            pnl2.Style.Add("height", "33.3%");
            pnl2.Style.Add("width", (Placa.Linhas[posLinha + 1].Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl2.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl2.Style.Add("background-size", "100%");

            lstPictogramas.AddRange(new List<Panel>() { pnl1, pnl2 });
          }
        }
        else
        {
          pnl1.Style.Add("height", "33.3%");
          pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl1.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

          pnl2.Style.Add("height", "33.3%");
          pnl2.Style.Add("width", (Placa.Linhas[posLinha + 1].Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl2.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl2.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

          pnl3.Style.Add("height", "33.3%");
          pnl3.Style.Add("width", (Placa.Linhas[posLinha + 2].Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl3.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl3.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

          lstPictogramas.AddRange(new List<Panel>() { pnl1, pnl2, pnl3 });
        }
      }
      else if (quantidadeAgrupamentos == 1)
      {
        pnlImagem.Style.Add("height",
          ((Placa.Linhas[posLinha].Height +
          Placa.Linhas[posLinha + 1].Height -
          linhaClass.Gap) / Divideby).toStringNumber() + "px");

        Panel pnl1 = new Panel();
        Panel pnl2 = new Panel();

        if (linhaClass.AgruparPictograma)
        {
          pnl1.Style.Add("height", "100%");
          pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl1.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");
          lstPictogramas.AddRange(new List<Panel>() { pnl1 });
        }
        else
        {
          pnl1.Style.Add("height", "50%");
          pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl1.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

          pnl2.Style.Add("height", "50%");
          pnl2.Style.Add("width", (Placa.Linhas[posLinha + 1].Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
          pnl2.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
          pnl2.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

          lstPictogramas.AddRange(new List<Panel>() { pnl1, pnl2 });
        }
      }
      else
      {
        if (posLinha < Placa.Linhas.Count)
        {
          if (Placa.Linhas[posLinha + 1].hasPictograma() >= 0)
          {
            pnlImagem.Style.Add("height",
               ((Placa.Linhas[posLinha].Height +
               Placa.Linhas[posLinha + 1].Height -
               linhaClass.Gap) / Divideby).toStringNumber() + "px");

            Panel pnl1 = new Panel();
            Panel pnl2 = new Panel();
            pnl1.Style.Add("height", "50%");
            pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl1.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

            pnl2.Style.Add("height", "50%");
            pnl2.Style.Add("width", (Placa.Linhas[posLinha + 1].Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl2.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl2.Style.Add("background-size", (linhaClass.Elements[y].Element.GetWidth() / Divideby) + "px");

            lstPictogramas.AddRange(new List<Panel>() { pnl1, pnl2 });
          }
          else
          {
            pnlImagem.Style.Add("height",
                ((Placa.Linhas[posLinha].Height +
                Placa.Linhas[posLinha + 1].Height -
                linhaClass.Gap) / Divideby).toStringNumber() + "px");

            Panel pnl1 = new Panel();
            pnl1.Style.Add("height", "100%");
            pnl1.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");
            pnl1.Style.Add("background", "url('" + linhaClass.Elements[y].Element.GetValue() + "') no-repeat center");
            pnl1.Style.Add("background-size", "100%");
            lstPictogramas.AddRange(new List<Panel>() { pnl1 });
          }
        }
      }

      pnlImagem.Style.Add("width", (linhaClass.Elements[y].Element.GetWidth() / Divideby).toStringNumber() + "px");

      lstPictogramas.ForEach(a => pnlImagem.Controls.Add(a));
      return pnlImagem;
    }
  }
}